layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: '/admin/Industry/index?action=ajax',
        method: 'post',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter',],
        cols: [[
            {type: "checkbox",},
            {field: 'title', title: '名称'},
            {field: 'level', title: '所属等级', sort: true},
            {field: 'min_worth', title: '基础价值'},
            {field: 'max_worth', title: '最高价值'},
            {field: 'total_num', title: '当前总数量', sort: true},
            {field: 'status_desc', title: '状态', sort: true,templet:"#statusTpl"},
            {title: '操作', toolbar: '#currentTableBar', minWidth: 120}
        ]],
        //limits: [10, 15, 20, 25, 50, 100],
        limit: 20,
        page: false,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        let result = JSON.stringify(data.field);
        //console.log(result)
        //执行搜索重载
        table.reload('currentTableId', {
            page: {curr: 1}
            , url:'/admin/Industry/index?action=ajax'
            , where: data.field
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {

        if (obj.event === 'add') {
            let index = layer.open({
                title: '添加行业',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/Industry/Saveinfo',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }
        else if (obj.event === 'delete') {

            let checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;

            let id='';
            for(let i in data)
            {
                id+=data[i].id+','
            }
            layer.confirm('真的删除行么',function(index){
                delajax(id,function (data) {
                    if (data.status) {
                        layer.msg('删除成功!', {icon: 1, time: 1000},function () {
                            $(".layui-form-checked").not('.header').parents('tr').remove();
                        });

                    }else{
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 3000
                        });
                    }

                })
            })

            // layer.alert(JSON.stringify(data));
        }

    });



    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            let index = layer.open({
                title: '编辑行业',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/Industry/Saveinfo?id='+obj.data.id,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }
        else if (obj.event === 'delete') {
            if(data.category!=1)
            {
                layer.msg('默认行业无法删除', {icon: 2, time: 1000})
                return false
            }

            layer.confirm('真的删除行么', function (index) {
                delajax(data.id,function (data) {
                    if (data.status) {
                        layer.msg('删除成功!', {icon: 1, time: 1000});
                        $(obj)[0].tr.remove()

                    }else{
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 3000
                        });
                    }
                })

                // $(obj)[0].tr.remove()
            });
        }
    });

    function delajax(ids,callback) {

        $.ajax({
            url: '/admin/Industry/DelInfo',
            type: 'post',
            dataType: 'json',
            data: {
                id: ids,
            },
            success: function(data) {
                callback(data)
            },
            error: function(re) {
                console.log(re)
                layer.open({
                    title: '删除失败',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        });
    }
});
